package com.alibaba.citrus.cr.common.util;

import java.util.Map;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.trace.TraceUtil;
import com.alibaba.fastjson.JSON;

import org.apache.commons.lang3.StringUtils;

/**
 * @author yunchow.zy
 * @since 2021/12/22 16:01
 */
public abstract class IntegrateUtils {
    private static final Log LOG = Log.getLogger(IntegrateUtils.class);

    private static final String DEFAULT_KEY_HEADER = "messageHeader";
    private static final String DEFAULT_KEY_DATA = "data";

    /**
     * HIGHLY RECOMMEND: Get the current message object of the messageClass type
     * @param messageClass The target message target type
     * @return null If message not exists with the current trace
     */
    public static <T> T safeGetMessageObject(Class<T> messageClass) {
        Map<String, Object> messageMap = safeGetCurrentContext();
        if (messageMap == null) {
            return null;
        }
        try {
            String jsonString = JSON.toJSONString(messageMap);
            if (jsonString == null) {
                return null;
            }
            return JSON.parseObject(jsonString, messageClass);
        } catch (Throwable e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
    }

    public static <T> T safeGetMessageHeader(Class<T> headerClass) {
        return safeGetMessageHeader(headerClass, DEFAULT_KEY_HEADER);
    }

    public static <T> T safeGetMessageHeader(Class<T> headerClass, String messageHeaderKey) {
        if (messageHeaderKey == null) {
            return null;
        }
        Map<String, Object> currentContext = safeGetCurrentContext();
        if (currentContext == null) {
            return null;
        }
        Object header = currentContext.get(messageHeaderKey);
        if (header == null) {
            return null;
        }
        T parseObject = null;
        try {
            String headerJson = JSON.toJSONString(header);
            if (StringUtils.isBlank(headerJson)) {
                return null;
            }
            parseObject = JSON.parseObject(headerJson, headerClass);
        } catch (Throwable e) {
            LOG.error(e.getMessage(), e);
        }
        return parseObject;
    }

    public static <T> T safeGetMessageData(Class<T> dataClass) {
        return safeGetMessageData(dataClass, DEFAULT_KEY_DATA);
    }

    public static <T> T safeGetMessageData(Class<T> dataClass, String dataKey) {
        if (dataKey == null) {
            return null;
        }
        Map<String, Object> currentContext = safeGetCurrentContext();
        if (currentContext == null) {
            return null;
        }
        Object data = currentContext.get(dataKey);
        if (data == null) {
            return null;
        }
        T parseObject = null;
        try {
            String dataJson = JSON.toJSONString(data);
            if (StringUtils.isBlank(dataJson)) {
                return null;
            }
            parseObject = JSON.parseObject(dataJson, dataClass);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return parseObject;
    }

    public static Map<String, Object> safeGetHeaderMap() {
        return safeGetHeaderMap(DEFAULT_KEY_HEADER);
    }

    public static Map<String, Object> safeGetHeaderMap(String headerKey) {
        Map<String, Object> currentContext = safeGetCurrentContext();
        if (currentContext == null) {
            return null;
        }
        Map<String, Object> headerMap = null;
        try {
            headerMap = (Map<String, Object>)currentContext.get(headerKey);
        } catch (Throwable e) {
            LOG.error(e.getMessage(), e);
        }
        return headerMap;
    }

    public static Object safeGetDataObject() {
        return safeGetDataObject(DEFAULT_KEY_DATA);
    }

    public static Object safeGetDataObject(String dataKey) {
        Map<String, Object> currentContext = safeGetCurrentContext();
        if (currentContext == null) {
            return null;
        };
        try {
            return currentContext.get(dataKey);
        } catch (Throwable e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
    }

    public static String safeGetDataJson() {
        return safeGetDataJson(DEFAULT_KEY_DATA);
    }

    public static String safeGetDataJson(String dataKey) {
        Map<String, Object> currentContext = safeGetCurrentContext();
        if (currentContext == null) {
            return null;
        };
        try {
            Object dataResult = currentContext.get(dataKey);
            if (dataResult == null) {
                return null;
            }
            return JSON.toJSONString(dataResult);
        } catch (Throwable e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
    }

    public static Map<String, Object> safeGetCurrentContext() {
        LOG.warn("IntegrateUtils.getCurrentContext enter");
        String traceId = null;
        Map<String, Object> resultMap = null;
        try {
            traceId = TraceUtil.getTraceId();
            //traceId = "0af907d616401655176718310d00cb";
            resultMap = SAPRequestUtils.getSAPRequestByTraceId(traceId);
        } catch (Throwable e) {
            LOG.error("IntegrateUtils get error", e);
        } finally {
            LOG.warn("IntegrateUtils.getCurrentContext exit: traceId=" + traceId + ",resultMap=" + resultMap);
        }
        return resultMap;
    }

    private IntegrateUtils() {
        // Stop new
    }

}